<template>
    <div class="category-box">
        <div class="category-action-bar">
            <el-button type="primary" size="mini" @click="openAdd">添加分类</el-button>
        </div>
        <div class="category-list-box">
            <el-table
                    v-loading="loading"
                    :data="category"
                    border
                    size="mini"
                    style="width: 100%">
                <el-table-column
                        fixed
                        prop="id"
                        label="ID"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="名称"
                        align="center"
                        width="150">
                </el-table-column>
                <el-table-column
                        prop="pinyin"
                        label="拼音"
                        align="center"
                        width="200">
                </el-table-column>
                <el-table-column
                        prop="status"
                        label="状态"
                        align="center"
                        width="120">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status == '1'">正常</el-tag>
                        <el-tag type="danger" v-if="scope.row.status == '0'">已删除</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="description"
                        label="描述"
                        align="center">
                </el-table-column>
                <el-table-column
                        prop="createTime"
                        label="创建时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="updateTime"
                        label="更新时间"
                        align="center"
                        width="180">
                </el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        align="center"
                        width="150">
                    <template slot-scope="scope">
                        <el-button type="primary" @click="handleEdit(scope.row.id)" size="small">编辑</el-button>
                        <el-button type="danger" v-if="scope.row.status !== '0'" @click="handleDelete(scope.row.id)"
                                   size="small">删除
                        </el-button>
                        <el-button type="danger" v-if="scope.row.status === '0'" disabled
                                   @click="handleDelete(scope.row.id)" size="small">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 子组件: 添加或编辑分类 -->
        <Edit
                :title="edit.title"
                :visible="edit.visible"
                :formData="edit.formData"
                :remoteClose="remoteClose"/>
        <Edit/>
    </div>
</template>

<script>
    import {categoryList, deleteCategoryById, getCategoryById} from '@/api/api';
    import Edit from './Edit'

    export default {
        name: "Categoty",
        components: {
            Edit
        },
        data() {
            return {
                loading: false,
                category: [],
                // 新增或编辑数据
                edit: {
                    title: '',
                    visible: false,
                    formData: {}
                }
            }
        },

        created() {
            this.fetchData()
        },
        methods: {
            // 获取分类列表数据
            fetchData() {
                this.loading = true
                categoryList().then(result => {
                    this.loading = false
                    if (result.code === 20000) {
                        this.category = result.data
                    }
                })
            },
            // 打开新增子窗口
            openAdd() {
                this.edit.visible = true
                this.edit.title = '添加分类'
                this.edit.formData = {}
            },
            // 关闭新增或编辑子窗口
            remoteClose() {
                this.edit.visible = false
                this.fetchData()
            },
            // 删除分类
            handleDelete(id) {
                this.$confirm('确定删除这条记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    // 发送删除请求
                    deleteCategoryById(id).then(response => {
                        // 刷新列表数据
                        this.fetchData()
                        // 处理响应结果提示
                        if (response.code === 20000) {
                            this.$message.success(response.message)
                        } else {
                            this.$message.error(response.message)
                        }
                    })
                }).catch(() => {
                    // 取消删除，不用理会
                })
            },
            // 根据ID获取分类
            async handleEdit(id) {
                const response = await getCategoryById(id)
                this.edit.formData = response.data
                this.edit.visible = true
                this.edit.title = '编辑分类'
            }
        }
    }
</script>
<style scoped>
    .category-box {
        padding: 5px;
    }

    .category-list-box {
        margin-top: 10px;
    }
</style>